package cn.com.agree.common.util;

import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.io.IOException;
import java.security.SecureRandom;

public class DESUtil {
	private final static String DES = "DES";
    private final static String ENCODE = "GBK";
    private final static String defaultKey = "cqnxcqnx";
    final static Base64 base64 = new Base64(); //base64鍔犲瘑
    public static void main(String[] args) throws Exception {
        String data = "娴嬭瘯ss21312312";
        // System.err.println(encrypt(data, key));
        // System.err.println(decrypt(encrypt(data, key), key));
        System.out.println(encrypt(data,""));
        System.out.println(decrypt(encrypt(data,""),""));

    }
    
    /**
     * 浣跨敤 榛樿key 鍔犲瘑
     * 
     * @return String
     * @author lifq
     * @date 2015-3-17 涓嬪崍02:46:43
     */
    public static String encrypt(String data,String key ) throws Exception {
    	if(key==null||"".equals(key)) {
        	key=defaultKey;
        }
        byte[] bt = encrypt(data.getBytes(ENCODE), key.getBytes(ENCODE));
        String strs = new String(base64.encode(bt));
        return strs;
    }

    /**
     * 浣跨敤 榛樿key 瑙ｅ瘑
     * 
     * @return String
     * @author lifq
     * @date 2015-3-17 涓嬪崍02:49:52
     */
    public static String decrypt(String data,String key) throws IOException, Exception {
        if (data == null)
            return null;
//        BASE64 decoder = new BASE64();
        
        if(key==null||"".equals(key)) {
        	key=defaultKey;
        }
        byte[] buf = Base64.decodeBase64(data.getBytes());
        byte[] bt = decrypt(buf, key.getBytes(ENCODE));
        return new String(bt, ENCODE);
    }
    
    
    /**
     * Description 鏍规嵁閿�艰繘琛屽姞瀵�
     * 
     * @param data
     * @param key
     *            鍔犲瘑閿産yte鏁扮粍
     * @return
     * @throws Exception
     */
    private static byte[] encrypt(byte[] data, byte[] key) throws Exception {
        // 鐢熸垚涓�涓彲淇′换鐨勯殢鏈烘暟婧�
        SecureRandom sr = new SecureRandom();

        // 浠庡師濮嬪瘑閽ユ暟鎹垱寤篋ESKeySpec瀵硅薄
        DESKeySpec dks = new DESKeySpec(key);

        // 鍒涘缓涓�涓瘑閽ュ伐鍘傦紝鐒跺悗鐢ㄥ畠鎶奃ESKeySpec杞崲鎴怱ecretKey瀵硅薄
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
        SecretKey securekey = keyFactory.generateSecret(dks);

        // Cipher瀵硅薄瀹為檯瀹屾垚鍔犲瘑鎿嶄綔
        Cipher cipher = Cipher.getInstance(DES);

        // 鐢ㄥ瘑閽ュ垵濮嬪寲Cipher瀵硅薄
        cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);

        return cipher.doFinal(data);
    }

    /**
     * Description 鏍规嵁閿�艰繘琛岃В瀵�
     * 
     * @param data
     * @param key
     *            鍔犲瘑閿産yte鏁扮粍
     * @return
     * @throws Exception
     */
    private static byte[] decrypt(byte[] data, byte[] key) throws Exception {
        // 鐢熸垚涓�涓彲淇′换鐨勯殢鏈烘暟婧�
        SecureRandom sr = new SecureRandom();

        // 浠庡師濮嬪瘑閽ユ暟鎹垱寤篋ESKeySpec瀵硅薄
        DESKeySpec dks = new DESKeySpec(key);

        // 鍒涘缓涓�涓瘑閽ュ伐鍘傦紝鐒跺悗鐢ㄥ畠鎶奃ESKeySpec杞崲鎴怱ecretKey瀵硅薄
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
        SecretKey securekey = keyFactory.generateSecret(dks);

        // Cipher瀵硅薄瀹為檯瀹屾垚瑙ｅ瘑鎿嶄綔
        Cipher cipher = Cipher.getInstance(DES);

        // 鐢ㄥ瘑閽ュ垵濮嬪寲Cipher瀵硅薄
        cipher.init(Cipher.DECRYPT_MODE, securekey, sr);

        return cipher.doFinal(data);
    }


}
